package com.itheima.dao.impl;

import com.itheima.dao.BaseTeacherDao;
import com.itheima.domain.Student;
import com.itheima.domain.Teacher;
import com.itheima.domain.User;
import com.itheima.util.JDBCUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TeacherDao_JDBC implements BaseTeacherDao {
    private static final Logger LOGGER = LoggerFactory.getLogger(StudentDao_JDBC.class);
    @Override
    public boolean addTeacher(Teacher teacher) {
        //获取数据库的连接
        Connection conn = JDBCUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<User> list = new ArrayList<>();
        try {
            conn = JDBCUtil.getConnection();

            //定义sql
            String sql = "INSERT INTO sm_teacher() VALUES (?,?,?,?)";

            //获取sql执行对象
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, teacher.getId());
            pstmt.setString(2, teacher.getName());
            pstmt.setString(3, teacher.getAge());
            pstmt.setString(4, teacher.getBirthday());

            //执行sql
            int affNum = pstmt.executeUpdate();

            if (affNum > 0){
                return true;
            }else {
                return false;
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        }finally {
            JDBCUtil.close(rs, pstmt, conn);
        }
        return false;
    }

    @Override
    public List<Teacher> findAllTeacher() {
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        List<Teacher> list = new ArrayList<>();
        try {
            conn = JDBCUtil.getConnection();
            //定义sql
            String sql = "SELECT * FROM sm_teacher";

            //获取sql执行对象
            psmt = conn.prepareStatement(sql);

            //执行sql
            rs = psmt.executeQuery();

            //处理结果
            while (rs.next()){
                String id = rs.getString("id");
                String name = rs.getString("name");
                String age = rs.getString("age");
                String birthday = rs.getString("birthday");
                list.add(new Teacher(id,name,age,birthday));
            }
        } catch (SQLException e) {
            LOGGER.error(e.getMessage());
        } finally {
            JDBCUtil.close(rs,psmt,conn);
        }

        return list;
    }

    @Override
    public boolean deleteTeacher(String delId) {
        Connection conn = null;
        PreparedStatement psmt = null;

        try {
            //获取数据库连接
            conn = JDBCUtil.getConnection();
            //定义sql
            String sql = "delete from sm_teacher where id = ?";

            //获取执行sql对象
            psmt = conn.prepareStatement(sql);
            psmt.setString(1,delId);

            //执行sql
            int affNum = psmt.executeUpdate();

            //结果处理
            if (affNum>0) {
                return true;
            }else
                return false;

        } catch (SQLException e) {
            LOGGER.error(e.getMessage());
        } finally {
            JDBCUtil.close(null,psmt,conn);
        }

        return false;
    }

    @Override
    public boolean updateTeacher(String updateId, Teacher newTeacher) {
        Connection conn = null;
        PreparedStatement psmt = null;

        try {
            //获取数据库连接
            conn = JDBCUtil.getConnection();
            //定义sql
            String sql = "update sm_teacher set name = ? , age = ? ,birthday = ? where id = ?";
            //获取sql执行对象
            psmt = conn.prepareStatement(sql);
            psmt.setString(1,newTeacher.getName());
            psmt.setString(2,newTeacher.getAge());
            psmt.setString(3,newTeacher.getBirthday());
            psmt.setString(4,updateId);

            //执行sql
            int affNum = psmt.executeUpdate();
            //结果处理
            if (affNum>0) {
                return true;
            }else
                return false;

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(null,psmt,conn);
        }
        return false;
    }

    @Override
    public int getIndex(String id) {
        return 0;
    }
}
